//
//  CompanyListFilterView.m
//  MyProject
//
//  Created by 石小强 on 2018/5/8.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "CompanyListFilterView.h"
#import "StringAndIndexModel.h"
#import "FilterButton.h"
#import "IndustryModel.h"

@interface CompanyListFilterView()

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIView *backGroundView;
@property (nonatomic, strong) UIView *topGroundView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) UIScrollView *backScrollView;

@end

@implementation CompanyListFilterView {
    NSString *_financingScale;
    NSString *_personnelScale;
    NSString *_industryId;
    IndustryModel *_industry;
}

- (void)show {
    // 1.获取最上面的窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.window = window;
    
    // 2.添加蒙版
    UIView *topGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, NAV_STATUS_H + 40)];
    topGroundView.backgroundColor = [UIColor clearColor];
    self.topGroundView = topGroundView;
    [window addSubview:topGroundView];
    UITapGestureRecognizer *tgr1= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissExt)];
    [topGroundView addGestureRecognizer:tgr1];
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, NAV_STATUS_H + 40, kScreenWidth, kScreenHeight - NAV_STATUS_H - 40)];
    backGroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    self.backGroundView = backGroundView;
    [window addSubview:backGroundView];
    UITapGestureRecognizer *tgr= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tgr];
    
    // 3.添加自己到蒙版上
    [backGroundView addSubview:self];
    
    // 4.设置尺寸
    self.frame = window.bounds;
    
    // 5.初始化子控件
    [self setupControls];
}

- (void)setupControls {
    
    CGFloat X = 15;
    NSArray *datas1 = @[@"不限", @"未融资", @"天使轮", @"A轮", @"B轮", @"C轮", @"D轮", @"已上市", @"不需要融资"];
    CGFloat column1 = 1;
    for (NSInteger i = 0; i < datas1.count; i++) {
        NSString *btnTitle = [datas1 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        
        if (X + btnTitleWidth + 15 > kScreenWidth) {
            X = 15;
            column1 += 1;
        }
        
        X += btnTitleWidth + 15;
    }
    NSArray *datas2 = @[@"不限", @"0-20人", @"20-99人", @"100-499人", @"500-999人", @"1000-9999人", @"10000人以上"];
    CGFloat column2 = 1;
    X = 15;
    for (NSInteger i = 0; i < datas2.count; i++) {
        NSString *btnTitle = [datas2 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        
        if (X + btnTitleWidth + 15 > kScreenWidth) {
            X = 15;
            column2 += 1;
        }
        X += btnTitleWidth + 15;
    }
    
    CGFloat column3 = 1;
    X = 15;
    for (NSInteger i = 0; i < self.industryArray.count; i++) {
        IndustryModel *model = [self.industryArray objectAtIndex:i];
        NSString *btnTitle = model.industryName;
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        
        if (X + btnTitleWidth + 15 > kScreenWidth) {
            X = 15;
            column3 += 1;
        }
        X += btnTitleWidth + 15;
    }
    
    
    // 286 + column * 42 + 49
    CGFloat resultH = 35 * 3 + (column1 + column2 + column3) * 42 + 15 + 49;
    CGFloat height = kScreenHeight - NAV_STATUS_H - 40 - VIEW_BOTTOM_H > resultH ? resultH : kScreenHeight - NAV_STATUS_H - 40 - VIEW_BOTTOM_H;
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
    self.backScrollView.backgroundColor = [UIColor whiteColor];
    self.backScrollView.contentSize = CGSizeMake(0, resultH);
    [self addSubview:self.backScrollView];
    
    UITapGestureRecognizer *tgr1= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(test)];
    [self.backScrollView addGestureRecognizer:tgr1];
    
    // 融资阶段
    UILabel *titleL1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 24, 100, 11)];
    titleL1.text = @"融资阶段";
    titleL1.textColor = UIColorFromRGB(0xA5B9C2);
    titleL1.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11];
    [self.backScrollView addSubview:titleL1];
    
    CGFloat btnY1 = 50;
    CGFloat btnX1 = 15;
    for (NSInteger i = 0; i < datas1.count; i++) {
        NSString *btnTitle = [datas1 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        if (btnX1 + btnTitleWidth + 15 > kScreenWidth) {
            btnX1 = 15;
            btnY1 += 42;
        }
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX1, btnY1, btnTitleWidth, 27)];
        btn.tag = 100 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX1 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
    }
    
    /** 人员规模 */
    UILabel *titleL2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 35 + column1 * 42 + 24, 100, 11)];
    titleL2.text = @"人员规模";
    titleL2.textColor = titleL1.textColor;
    titleL2.font = titleL1.font;
    [self.backScrollView addSubview:titleL2];
    
    
    CGFloat btnY2 = 35 + column1 * 42 + 35 + 15;
    CGFloat btnX2 = 15;
    for (NSInteger i = 0; i < datas2.count; i++) {
        NSString *btnTitle = [datas2 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        if (btnX2 + btnTitleWidth + 15 > kScreenWidth) {
            btnX2 = 15;
            btnY2 += 42;
        }
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX2, btnY2, btnTitleWidth, 27)];
        btn.tag = 110 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX2 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
    }
    
    /** 行业 */
    UILabel *titleL3 = [[UILabel alloc] initWithFrame:CGRectMake(15, (column2 + column1) * 42 + 70 + 24, 260, 11)];
    titleL3.text = @"行业";
    titleL3.textColor = titleL1.textColor;
    titleL3.font = titleL1.font;
    [self.backScrollView addSubview:titleL3];
    NSArray *datas3 = self.industryArray;
    CGFloat btnY3 = (column2 + column1) * 42 + 70 + 24 + 15;
    CGFloat btnX3 = 15;
    for (NSInteger i = 0; i < datas3.count; i++) {
        IndustryModel *model = [datas3 objectAtIndex:i];
        NSString *btnTitle = model.industryName;
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        
        if (btnX3 + btnTitleWidth + 15 > kScreenWidth) {
            btnX3 = 15;
            btnY3 += 27 + 15;
        }
        
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX3, btnY3, btnTitleWidth, 27)];
        btn.tag = 120 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX3 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
        
    }
    
    // 确认按钮
    UIButton *okBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, resultH - 49, kScreenWidth, 49)];
    [okBtn setBackgroundColor:UIColorFromRGB(0x00C6D5)];
    [okBtn setTitle:@"确认" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    okBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17];
    [okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backScrollView addSubview:okBtn];
}

- (void)btn1Click:(FilterButton *)btn {
    if (btn.selected) {
        return;
    } else {
        btn.selected = YES;
        if (btn.tag >= 100 && btn.tag < 110) {
            _financingScale = [self getFinancingScaleWithTag:btn.tag - 100];
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 100 && cont.tag < 110 && cont.tag != btn.tag) {    // 融资阶段
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        } else if (btn.tag >= 110 && btn.tag < 120) { // 人员规模
            _personnelScale = [self getPersonalScaleWithTag:btn.tag - 120];
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 110 && cont.tag < 120 && cont.tag != btn.tag) {    // 工作经验
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        } else if (btn.tag >= 120) { // 行业
            _industry = [self.industryArray objectAtIndex:btn.tag - 120];
            _industryId = _industry.industryId;
            
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 120 && cont.tag != btn.tag) {    // 工作经验
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        }
        
    }
}

- (NSString *)getFinancingScaleWithTag:(NSInteger)tag {
    if (tag == 0) {
        return @"";
    } else if (tag == 1) {
        return @"COMPANY_NATURE_NOT_FINANCING";
    } else if (tag == 2) {
        return @"COMPANY_NATURE_AI";
    } else if (tag == 3) {
        return @"COMPANY_NATURE_PE_A";
    } else if (tag == 4) {
        return @"COMPANY_NATURE_PE_B";
    }  else if (tag == 5) {
        return @"COMPANY_NATURE_PE_C";
    }  else if (tag == 6) {
        return @"COMPANY_NATURE_PE_D";
    }  else if (tag == 7) {
        return @"COMPANY_NATURE_LAUNCHED";
    }  else {
        return @"COMPANY_NATURE_UNWANTED_FINANCING";
    }
}

- (NSString *)getPersonalScaleWithTag:(NSInteger)tag {
    if (tag == 0) {
        return @"";
    } else if (tag == 1) {
        return @"COMPANY_SCALE_GT_0_LT_20";
    } else if (tag == 2) {
        return @"COMPANY_SCALE_GT_20_LT_99";
    } else if (tag == 3) {
        return @"COMPANY_SCALE_GT_100_LT_499";
    } else if (tag == 4) {
        return @"COMPANY_SCALE_GT_500_LT_999";
    } else if (tag == 5) {
        return @"COMPANY_SCALE_GT_1000_LT_9999";
    } else {
        return @"COMPANY_SCALE_GT_10000";
    }
}

- (void)okBtnClick {
    [self dismiss];
}

- (void)dismiss {
    [self.backScrollView removeFromSuperview];
    [self.backGroundView removeFromSuperview];
    [self.topGroundView removeFromSuperview];
    self.dismissBlock(_financingScale, _personnelScale, _industryId);
    [self removeFromSuperview];
}

- (void)dismissExt {
    [self.backScrollView removeFromSuperview];
    [self.backGroundView removeFromSuperview];
    [self.topGroundView removeFromSuperview];
    self.justDismissBlock();
    [self removeFromSuperview];
}

- (void)test {
    NSLog(@"点击了scrollview");
}

@end
